﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Zxb.DayWork.Core;

namespace Zxb.DayWork.Logic
{
    public class RealEstateWorker : BatchLoopWorker
    {
        DataTable sourceTable;
        DataTable targetTable;
        NorthSouth ns;
        int num;
        int startid;
        bool hasNew = true;

        public RealEstateWorker(NorthSouth northsouth, int pageCount)
        {
            ns = northsouth;
            num = pageCount;
        }

        protected override void ReadData()
        {
            try
            {
                Database sourcedb = Constant.DicNorthSouth[ns].DataBase;
                string sql = string.Format("select top {0} NID,Newcode,ProjName,proj_type,Province,City,District,ComArea,Address,PartTotalDoor,TotalDoor,LivinDate,PurposeArea,BuildCategory,FixStatus from proj_info with(nolock) where NID>{1} order by NID", num, startid);
                DbCommand dbCommand = sourcedb.GetSqlStringCommand(sql);
                sourceTable = sourcedb.ExecuteDataSet(dbCommand).Tables[0];
            }
            catch (Exception ex)
            {
                Logger.InfoLog.Error(string.Concat("读取新房表proj_info出错：", ex.Message));
            }
        }

        protected override void ProcessData()
        {
            if (sourceTable == null || sourceTable.Rows.Count == 0)
            {
                return;
            }

            targetTable = PrepareTableSchemaUdtEstateManage();

            foreach (DataRow dr in sourceTable.Rows)
            {
                long estID = 0;
                long.TryParse(dr["Newcode"].ToString(), out estID);
                DateTime acceptTime = new DateTime(1901, 1, 1);
                DateTime.TryParse(dr["LivinDate"].ToString(), out acceptTime);
                int nid = 0;
                int.TryParse(dr["NID"].ToString(), out nid);
                int acceptuser = 0;
                int.TryParse(dr["PartTotalDoor"].ToString(), out acceptuser);
                int totaluser = 0;
                int.TryParse(dr["TotalDoor"].ToString(), out totaluser);
                decimal estArea = 0.0M;
                decimal.TryParse(dr["PurposeArea"].ToString(), out estArea);

                DataRow targetRow = targetTable.NewRow();
                targetRow["RealEstateID"] = estID;
                targetRow["RealEstateName"] = dr["ProjName"].ToString();
                targetRow["NewHouseRealEstateID"] = nid;
                targetRow["NorthSouth"] = ns.ToString();

                targetRow["ProjType"] = dr["Proj_Type"].ToString();
                targetRow["Province"] = dr["Province"].ToString();
                targetRow["City"] = dr["City"].ToString();
                targetRow["District"] = dr["District"].ToString();
                targetRow["ComArea"] = dr["ComArea"].ToString();
                targetRow["Addr"] = dr["Address"].ToString();

                targetRow["AcceptUserNumber"] = acceptuser;
                targetRow["TotalUserNumber"] = totaluser;
                targetRow["AcceptTime"] = acceptTime;
                targetRow["PurposeArea"] = estArea;
                targetRow["BuildCategory"] = dr["BuildCategory"].ToString();
                targetRow["FixStatus"] = dr["FixStatus"].ToString();

                targetTable.Rows.Add(targetRow);
            }
        }

        public static DataTable PrepareTableSchemaUdtEstateManage()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[]
            { 
                new DataColumn("RealEstateID",typeof(Int64)), 
                new DataColumn("RealEstateName",typeof(string)), 
                new DataColumn("NewHouseRealEstateID",typeof(int)),
                new DataColumn("NorthSouth",typeof(string)), 

                new DataColumn("ProjType",typeof(string)), 
                new DataColumn("Province",typeof(string)), 
                new DataColumn("City",typeof(string)), 
                new DataColumn("District",typeof(string)), 
                new DataColumn("ComArea",typeof(string)), 
                new DataColumn("Addr",typeof(string)), 

                new DataColumn("AcceptUserNumber",typeof(Int32)),
                new DataColumn("TotalUserNumber",typeof(Int32)),
                new DataColumn("AcceptTime",typeof(DateTime)), 
                new DataColumn("PurposeArea",typeof(decimal)), 
                new DataColumn("BuildCategory",typeof(string)), 
                new DataColumn("FixStatus",typeof(string))
            });
            return dt;
        }

        protected override void WriteData()
        {
            hasNew = DAL.TVPInsertEstate(targetTable, ns);
        }

        protected override void InitLoopCondition()
        {
            startid = DAL.GetLastID(Constant.DicNorthSouth[ns].StatusName);
            if (startid == -1)
            {
                hasNew = false;
                Logger.InfoLog.Error(string.Concat("读取sitestatus出错：", ns.ToString()));
                return;
            }
        }

        protected override bool CanContinue()
        {
            return hasNew;
        }

        protected override void UpdateLoopCondition()
        {
            sourceTable = null;
            targetTable = null;
            if (hasNew)
            {
                startid = DAL.GetLastID(Constant.DicNorthSouth[ns].StatusName);
            }
        }
    }
}
